package ve.usb.coord.colision;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.coord.colision.hibernate.*;


/**
 * Manejo de elementos de la entidad Grupo.<br>
 */
public class AccionesGrupo extends CohesionAction {
    
/**
     * Acción llamada por Struts para ejecutar la acción GrupoPlanDeEstudiosCrearI.
     * Prepara la vista de captura de datos de una entidad para realizar una inserción.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward GrupoPlanDeEstudiosCrearI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "grupoPlanDeEstudiosCrear";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            HttpSession sess = request.getSession();
            FGrupo fFGrupo = (FGrupo)sess.getAttribute("FGrupo");
            if (fFGrupo==null) {
                fFGrupo = new FGrupo();
                sess.setAttribute("FGrupo", fFGrupo);
            } else {
                fFGrupo.reset(mapping, request);
            }
             
            /*Feed for combo listPlanDeEstudios.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(coordinacion, idPlan) " + 
               "from PlanDeEstudios").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFGrupo.setListPlanDeEstudios(list);
            }
             */

            fFGrupo.setPlanDeEstudios(Long.parseLong((String)request.getParameter("idPlan")));

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción GrupoI.
     * Prepara la vista de actualización de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward GrupoI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "grupo";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String id = request.getParameter("idGrupo");
            Long idGrupo = (id==null) ? 
                (Long)request.getSession().getAttribute("idGrupo") :
                new Long(id);
            
            HttpSession sess = request.getSession();
            FGrupo fFGrupo = (FGrupo)sess.getAttribute("FGrupo");
            if (fFGrupo==null) {
                fFGrupo = new FGrupo();
                sess.setAttribute("FGrupo", fFGrupo);
            } else {
                fFGrupo.reset(mapping, request);
            }
            Grupo grupo = (Grupo)s.load(Grupo.class, idGrupo);
            fFGrupo.setIdGrupo(grupo.getIdGrupo());

            fFGrupo.setPlanDeEstudios(grupo.getPlanDeEstudios().getIdPlan());

            fFGrupo.setNombre(grupo.getNombre());
            fFGrupo.setPrioridad(grupo.getPrioridad());
            request.getSession().setAttribute("prioridad", grupo.getPrioridad());

            //Feed for combo listPlanDeEstudios.
            /*
             {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(coordinacion, idPlan) " + 
               "from PlanDeEstudios").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFGrupo.setListPlanDeEstudios(list);
            }
             */

            for (Requisitos _Requisitos : grupo.getRequisitos()) {
                Hibernate.initialize(_Requisitos.getAsignatura());
            }
            request.setAttribute("L_Requisitos", grupo.getRequisitos());

            request.getSession().setAttribute("idPlan", grupo.getPlanDeEstudios().getIdPlan());

            request.getSession().setAttribute("idGrupo", idGrupo);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción GrupoAct.
     * Registro de actualizaciones de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward GrupoAct(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "grupo";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FGrupo fFGrupo = (FGrupo)form;

            Grupo grupo = (Grupo)s.load(Grupo.class, new Long(fFGrupo.getIdGrupo()));
            grupo.setNombre(fFGrupo.getNombre());
            grupo.setPrioridad(fFGrupo.getPrioridad());
            request.getSession().setAttribute("prioridad", grupo.getPrioridad());


            //Feed for combo listPlanDeEstudios.
            /*
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(coordinacion, idPlan) " + 
               "from PlanDeEstudios").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFGrupo.setListPlanDeEstudios(list);
            }
             */

            for (Requisitos _Requisitos : grupo.getRequisitos()) {
                Hibernate.initialize(_Requisitos.getAsignatura());
            }
            request.setAttribute("L_Requisitos", grupo.getRequisitos());

            request.getSession().setAttribute("idPlan", grupo.getPlanDeEstudios().getIdPlan());
            request.getSession().setAttribute("idGrupo", grupo.getIdGrupo());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción GrupoElim.
     * Elimina la entidad seleccionada.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward GrupoElim(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "PlanDeEstudiosI";
        final String SALIDA_1 = "grupo";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String id = request.getParameter("idGrupo");
            Long idGrupo = (id==null) ? 
                (Long)request.getSession().getAttribute("idGrupo") :
                new Long(id);
            Grupo grupo = (Grupo)s.load(Grupo.class, idGrupo);

            long idPlan = grupo.getPlanDeEstudios().getIdPlan();
            grupo.getPlanDeEstudios().removeFromGrupos(grupo);

            try {
                s.flush();
                request.getSession().setAttribute("idGrupo", idGrupo);

                request.getSession().setAttribute("idPlan", idPlan);

            } catch (Exception ex1) {
                ex1.printStackTrace();
                salida = SALIDA_1;
                request.setAttribute("msg", "No se pudo eliminar entidad.");
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción GrupoCrear.
     * Registro de la creación de una grupo.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward GrupoCrear(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_1 = "grupo";

        String salida = SALIDA_1;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FGrupo fFGrupo = (FGrupo)form;

            Grupo grupo = new Grupo();
            grupo.setNombre(fFGrupo.getNombre());
            grupo.setPrioridad(fFGrupo.getPrioridad());

            {PlanDeEstudios obj = (PlanDeEstudios)s.load(PlanDeEstudios.class, new Long(fFGrupo.getPlanDeEstudios()));
             obj.addToGrupos(grupo);
            }


            s.flush();
            fFGrupo.setIdGrupo(grupo.getIdGrupo());
            /*Feed for combo listPlanDeEstudios.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(coordinacion.nombre, idPlan) " + 
               "from PlanDeEstudios").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFGrupo.setListPlanDeEstudios(list);
            }
             */


            //fFGrupo.setPlanDeEstudios(grupo.getPlanDeEstudios().getIdPlan());
            request.getSession().setAttribute("idPlan", fFGrupo.getPlanDeEstudios());
            request.getSession().setAttribute("prioridad", grupo.getPrioridad());
            request.getSession().setAttribute("idGrupo", grupo.getIdGrupo());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }



}